import tkinter as tk
from tkinter import messagebox
from eventcenter import eventcenter as event
import constants as const
class Tools:
    _instance = None
    
    def __new__(cls, root=None):
        if cls._instance is None:
            cls._instance = super(Tools, cls).__new__(cls)
        if root is not None:
            cls._instance.root = root
        return cls._instance
        
    def __init__(self, root=None):
        # 初始化方法不需要做任何事，因为在__new__中已经处理了
        pass
        
    def show_tip_info(self, info):
        event.dispatch_event(const.event_show_tip_info, info)

    def toast(self, message):
        event.dispatch_event(const.event_show_toast, message)
    
    def convert_to_centered(self, x, y):
        # 计算游戏区域的中心点在canvas坐标系中的位置
        rect_x2 = const.game_window_width - const.fix_distance
        rect_y2 = const.game_window_height - const.fix_distance
        center_x = (const.fix_distance + rect_x2) / 2
        center_y = (const.fix_distance + rect_y2) / 2
        
        # 将卡片左上角坐标转换为卡片中心点坐标
        card_center_x = x + const.card_width / 2
        card_center_y = y + const.card_height / 2
        
        # 计算卡片中心点相对于游戏区域中心点的偏移
        nx = card_center_x - center_x
        ny = center_y - card_center_y  # y轴方向相反，向上为正

        return nx,ny

    def convert_to_left(self, x, y):
        # 计算游戏区域的中心点在canvas坐标系中的位置
        rect_x2 = const.game_window_width - const.fix_distance
        rect_y2 = const.game_window_height - const.fix_distance
        center_x = (const.fix_distance + rect_x2) / 2
        center_y = (const.fix_distance + rect_y2) / 2
        
        # 将中心点坐标系中的坐标转换回左上角坐标系
        # 注意：y轴方向相反，在中心点坐标系中向上为正
        card_center_x = x + center_x
        card_center_y = center_y - y
        
        # 将卡片中心点坐标转换为卡片左上角坐标
        left_x = card_center_x - const.card_width / 2
        left_y = card_center_y - const.card_height / 2
        
        return left_x, left_y

tools = Tools()